package icraf.carbon.gui.model;

import icraf.carbon.core.CostBenefitUnit;
import icraf.carbon.gui.LCMatrixTabPanel;
import icraf.carbon.gui.MenuFactory.MenuProperty;
import icraf.carbon.gui.OutputEmissionNPVPanel;
import icraf.carbon.model.ModelProject;
import icraf.common.gui.VerticalLabelUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.util.ArrayList;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeListener;

public class IterationEmissionNPVPanel extends JPanel implements InstantCounter<IterationEmissionNPVPanel>{
	


	
	
	
	private static final long serialVersionUID = 3935329706944049274L;

	private ModelProject modelProject;
	JTabbedPane tabPanel = new JTabbedPane(JTabbedPane.LEFT);
	private ArrayList<ChangeListener> changeListenerList = new ArrayList<ChangeListener>();
	
	public IterationEmissionNPVPanel() {
		super();
		tabPanel.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
		this.setLayout(new BorderLayout());
		this.add(tabPanel);
		JLabel l = new JLabel(MenuProperty.MODEL_PERIOD.getLabel()+ " ");
		l.setFont(new Font("Arial", Font.BOLD, 16));
		l.setOpaque(true);
		l.setBackground(Color.gray);
		l.setForeground(Color.white);
		l.setUI(new VerticalLabelUI());
		l.setHorizontalAlignment(JLabel.RIGHT);
		this.add(l, BorderLayout.WEST);
		
	}

	public void fireTableDataChanged() {
		LCMatrixTabPanel mPanel;
		for (int i = 0; i < tabPanel.getTabCount(); i++) {
			mPanel = (LCMatrixTabPanel) tabPanel.getComponentAt(i);
			mPanel.fireTableDataChanged();
		}
	}

	public ModelProject getModelProject() {
		return modelProject;
	}

	public int getSelectedTabIndex() {
		return tabPanel.getSelectedIndex();
	}

	public JTabbedPane getTabPanel() {
		return tabPanel;
	}


	
	
	
//	public void setAreaScale(boolean b) {
//		LCMatrixTabPanel mPanel;
//		for (int i = 0; i < tabPanel.getTabCount(); i++) {
//			mPanel = (LCMatrixTabPanel) tabPanel.getComponentAt(i);
//			mPanel.setAreaScale(b);
//		}
//	}
	
	
	public void setModelProject(ModelProject modelProject, CostBenefitUnit attributs) {
		this.modelProject = modelProject;
		int n = -1;
		if(modelProject != null) {
			n = modelProject.getIterationCount();
		}
		
		while (tabPanel.getTabCount() > n + 1) {
			tabPanel.remove(tabPanel.getTabCount() - 1);
		}
		OutputEmissionNPVPanel mPanel;
		for (int i = 0; i < n + 1; i++) {
			if (i < tabPanel.getTabCount()) {
				mPanel = (OutputEmissionNPVPanel) tabPanel.getComponentAt(i);
				tabPanel.setTitleAt(i, Integer.toString(i));
			} else {
				mPanel = new OutputEmissionNPVPanel();
				tabPanel.addTab(Integer.toString(i), mPanel);

				
			}
			tabPanel.setEnabledAt(i, true);
			mPanel.setProject(modelProject.getSimulatedProject(i),  attributs);
			
			
			
		}

	}


	
	public void addZoneTabListener(ChangeListener l) {
		changeListenerList.add(l);
		LCMatrixTabPanel mPanel;
		for (int i = 0; i < tabPanel.getTabCount(); i++) {
			mPanel = (LCMatrixTabPanel) tabPanel.getComponentAt(i);
			mPanel.addZoneTabListener(l);
		}
	}
	
	public void selectZoneTab(int idx) {
		LCMatrixTabPanel mPanel;
		for (int i = 0; i < tabPanel.getTabCount(); i++) {
			mPanel = (LCMatrixTabPanel) tabPanel.getComponentAt(i);
			mPanel.selectZoneTab(idx);
		}
	}

//	@Override
//	public void setProject(Project project, DataType t, Object attributes) {
//		setModelProject(project.getModelProject(), t, (CostBenefitUnit)attributes);
//	}
	
	

	@Override
	public IterationEmissionNPVPanel createInstant() {
		return new IterationEmissionNPVPanel();
	}
	
	private Component topComponent;
	public void setTopComponent(Component c) {
		if(topComponent != null)
			this.remove(topComponent);
		this.add(c, BorderLayout.NORTH);
		topComponent = c;
	}

}